<template>
  <div v-if="nodeIds.length !== 0">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <div class="search">
        <el-form-item label="用例名称" prop="name">
          <el-input v-model.trim="queryParams.name" placeholder="请输入用例名称搜索" clearable/>
        </el-form-item>
        <el-form-item label="用例描述" prop="description">
          <el-input v-model.trim="queryParams.description" placeholder="请输入用例描述搜索" clearable/>
        </el-form-item>
      </div>
      <div class="search">
        <el-form-item label="用例等级" prop="level">
          <el-select v-model="queryParams.level" placeholder="请选择用例等级">
            <el-option label="P0" value="0"></el-option>
            <el-option label="P1" value="1"></el-option>
            <el-option label="P2" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建人" prop="createUser">
          <el-input v-model.trim="queryParams.createUser" placeholder="请输入创建人名称搜索" clearable/>
        </el-form-item>
      </div>
      <div class="search">
        <el-button type="primary" @click="restList(query.keywords)" icon="el-icon-search">查询</el-button>
        <el-button @click="refresh('queryForm')" icon="el-icon-refresh">重置</el-button>
      </div>
    </el-form>
    <div class="create-btn">
      <el-button type="primary" icon="el-icon-plus" @click="goCUView('' +'新建用例')" v-if="isReference" v-permission="'case:add'">新建用例</el-button>
    </div>
    <div class="create-btn">
      <el-table :data="tableData" element-loading-spinner="el-icon-loading" element-loading-text="加载中。。。"
                element-loading-custom-class="equipment-loading" element-loading-background="rgba(55,55,55,0.2)"
                height="calc(100vh - 419px)">
        <el-table-column fixed prop="num" label="序号" width="100"></el-table-column>
        <el-table-column fixed prop="name" label="用例名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="description" label="用例描述" show-overflow-tooltip></el-table-column>
        <el-table-column prop="level" label="是否被引用" width="100">
          <template slot-scope="scope">
            {{ scope.row.isReference === 1 ?  "是" :  "否" }}
          </template>
        </el-table-column>
        <el-table-column prop="level" label="用例等级" width="100">
          <template slot-scope="scope">
            {{ scope.row.level === "0" ? "P0" : scope.row.level === "1" ? "P1" : "P2" }}
          </template>
        </el-table-column>
        <el-table-column prop="createUser" label="创建人" width="100"></el-table-column>
        <!--        <el-table-column prop="createTime" label="创建时间" width="150"></el-table-column>-->
        <el-table-column prop="updateTime" label="更新时间" width="150"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template v-slot="scope">
            <el-button type="text" @click="referenceCase(scope.row)" v-if="!isReference">引用</el-button>
            <el-button type="text" @click="goCUView('编辑用例',scope.row)" v-if="isReference" v-permission="'case:update'">编辑</el-button>
            <el-popconfirm
              @onConfirm="handleDelete(scope.$index, scope.row)"
              confirm-button-text='确定'
              cancel-button-text='我再看看'
              icon="el-icon-info"
              icon-color="red"
              title="是否确认删除？"
            >
              <el-button slot="reference" type="text" v-if="isReference" v-permission="'case:delete'">删除</el-button>
            </el-popconfirm>
            <el-button type="text" @click="copyCase(scope.row)" v-if="isReference" v-permission="'case:copy'">克隆</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="query.pageNum"
        :limit.sync="query.pageRow"
        layout="total, prev, pager, next"
        @pagination="getCaseList"
      />
    </div>
  </div>

</template>

<script>

import Pagination from "@/components/Pagination/index";

export default {
  name: "case",
  props: ['nodeIds'],
  components: {Pagination},
  data() {
    return {
      isReference: false,
      queryParams: {
        name: "",
        level: "",
        createUser: "",
        description: ""
      },
      tableData: [],
      total: 0,
      query: {
        pageRow: 10,
        pageNum: 1,
        projectId: sessionStorage.getItem("projectId")
      },
      visible: false,
      title: "",
      editData: {}
    }
  },
  watch: {
    nodeIds() {
      sessionStorage.setItem("sceneId", this.nodeIds[0])
      const loading = this.$loading({
        lock: true, // lock的修改符--默认是false
        text: 'Loading', // 显示在加载图标下方的加载文案
        spinner: 'el-icon-loading', // 自定义加载图标类名
        background: 'rgba(0, 0, 0, 0.1)', // 遮罩层颜色
        target: document.querySelector('#table')//loading覆盖的dom元素节点
      })
      this.getCaseList()
      loading.close()
    }
  },
  mounted() {
    if (this.$route.path === "/ui/scene") {
      this.isReference = true;
    } else {
      this.isReference = false;
    }
  },
  methods: {
    restList() {
      this.query.pageNum = 1;
      this.getCaseList();
    },
    refresh(queryForm) {
      this.$refs[queryForm].resetFields();
      this.getCaseList();
    },
    getCaseList() {
      this.query = {...this.query, ...this.queryParams}
      this.query["sceneId"] = this.nodeIds
      this.api({
        url: "/getCaseList",
        method: "post",
        data: this.query
      }).then(resp => {
        this.tableData = resp.list;
        this.total = resp.totalCount;
      });
    },
    copyCase(data) {
      let body = {
        name: data.name + "_copy",
        sceneId: data.sceneId,
        level: data.level,
        description: data.description,
        steps: data.steps,
        isHandless: data.isHandless,
        isEndClose: data.isEndClose,
        isContinueRun: data.isContinueRun,
        isShot: data.isShot,
        projectId: data.projectId,
        createUser: data.createUserId,
        updateUser: data.updateUserId
      }
      this.api({url: "/addCase", method: "post", data: body}).then(resp => {
        this.$message({message: resp, type: 'success'});
        this.getCaseList()
      })
    },
    handleDelete(index, data) {
      this.query.pageNum = 1
      this.api({url: `/deleteCase/${data.id}`, method: "delete"}).then((resp) => {
        this.getCaseList()
        this.$message({message: resp, type: 'success'});
      })
    },
    referenceCase(data) {
      this.$emit("referenceData", data);
    },
    goCUView(title, data = null) {
      if (data) {
        sessionStorage.setItem("caseId", data.id)
        sessionStorage.setItem("caseName", data.name)
      } else {
        sessionStorage.removeItem("caseId")
      }
      sessionStorage.removeItem("planId")
      this.$router.push({path: 'cuView', query: {title: title}})
    }
  }
}
</script>

<style scoped lang="scss">
.search {
  margin: auto;
  text-align: center;
}

.create-btn {
  margin-top: 10px;
}
</style>
